/*
 * @Author: zwz
 * @Date: 2023-05-28 16:27:48
 * @LastEditors: zwz
 * @LastEditTime: 2023-06-07 14:59:50
 * @Description: 注册
 */

const JSEncrypt = require('node-jsencrypt')
const { Controller } = require('egg');
const vUser = {
    username: { type: 'string', required: true },
    password: { type: 'string', required: true },
    age: {type: 'number', required: true}
};
class SignInController extends Controller {
    async index() {
       const { ctx, app} = this
       // 接收并校验参数
        ctx.validate(vUser, ctx.request.body);
       const {username,password} = ctx.request.body
       const users = await ctx.service.user.checkUserName(ctx.request.body);
       if (users.length !== 0) {
        ctx.failure("用户已经注册",{},200432);
        return
       }
       await ctx.service.user.Rigist(ctx.request.body);
       const token = app.jwt.sign({
        username: username, //需要存储的 token 数据
       }, app.config.jwt.secret);
       ctx.success('注册成功!',{token: token,username},200);
    }
}
module.exports = SignInController;